package org.third.integration.hibernate;

import org.junit.Test;
import org.postgresql.Driver;

import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;

public class DbPostgreSQLTest extends DbTest {

    public static void main(String[] args) {
        new DbPostgreSQLTest().testDatabase();
    }

    /**
     * <pre>
     * &#64;Column(length = 20 , nullable = false , unique = true , columnDefinition="nvarchar(20)"): datatabase compliance  
    
    
     * public class SQLServerNativeDialect extends SQLServerDialect {
     *     public SQLServerNativeDialect() {
     *         super();
     *         registerColumnType(Types.VARCHAR, "nvarchar($l)");
     *         registerColumnType(Types.CLOB, "nvarchar(max)");
     *     }
     * 
     *     public String getTypeName(int code, int length, int precision, int scale) throws HibernateException {
     *         if (code != 2005) {
     *             return super.getTypeName(code, length, precision, scale);
     *         } else {
     *             return "ntext";
     *         }
     *     }
     * }
     * 
     * { "PGDBNAME", Boolean.TRUE,  "Database name to connect to; may be specified directly in the JDBC URL." },
        { "user", Boolean.TRUE,  "Username to connect to the database as.", null },
        { "PGHOST", Boolean.FALSE,  "Hostname of the PostgreSQL server; may be specified directly in the JDBC URL." },
        { "PGPORT", Boolean.FALSE,  "Port number to connect to the PostgreSQL server on; may be specified directly in the JDBC URL.", },
        { "password", Boolean.FALSE,  "Password to use when authenticating.", },
        { "protocolVersion", Boolean.FALSE,  "Force use of a particular protocol version when connecting; if set, disables protocol version fallback.", },
        { "ssl", Boolean.FALSE,  "Control use of SSL; any nonnull value causes SSL to be required." },
        { "sslfactory", Boolean.FALSE,  "Provide a SSLSocketFactory class when using SSL." },
        { "sslfactoryarg", Boolean.FALSE,  "Argument forwarded to constructor of SSLSocketFactory class." },
        { "loglevel", Boolean.FALSE,  "Control the driver's log verbosity: 0 is OFF, 1 is INFO, 2 is DEBUG.",  new String[] { "0", "1", "2" } },
        { "allowEncodingChanges", Boolean.FALSE,  "Allow the user to change the client_encoding variable." },
        { "logUnclosedConnections", Boolean.FALSE,  "When connections that are not explicitly closed are garbage collected, log the stacktrace from the opening of the connection to trace the leak source."},
        { "prepareThreshold", Boolean.FALSE,  "Default statement prepare threshold (numeric)." },
        { "binaryTransfer", Boolean.FALSE,  "Use binary format for sending and receiving data if possible." },
        { "binaryTransferEnable", Boolean.FALSE,  "Comma separated list of types to enable binary transfer. Either OID numbers or names." },
        { "binaryTransferDisable", Boolean.FALSE,  "Comma separated list of types to disable binary transfer. Either OID numbers or names. Overrides values in the driver default set and values set with binaryTransferEnable." },
        { "charSet", Boolean.FALSE,  "When connecting to a pre-7.3 server, the database encoding to assume is in use." },
        { "compatible", Boolean.FALSE,  "Force compatibility of some features with an older version of the driver.",  new String[] { "7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2" } },
        { "loginTimeout", Boolean.FALSE,  "The login timeout, in seconds; 0 means no timeout beyond the normal TCP connection timout." },
        { "socketTimeout", Boolean.FALSE,  "The timeout value for socket read operations, in seconds; 0 means no timeout." },
        { "tcpKeepAlive", Boolean.FALSE,  "Enable or disable TCP keep-alive probe." },
        { "stringtype", Boolean.FALSE,  "The type to bind String parameters as (usually 'varchar'; 'unspecified' allows implicit casting to other types)",  new String[] { "varchar", "unspecified" } },
        { "kerberosServerName", Boolean.FALSE,  "The Kerberos service name to use when authenticating with GSSAPI.  This is equivalent to libpq's PGKRBSRVNAME environment variable." },
        { "jaasApplicationName", Boolean.FALSE,  "Specifies the name of the JAAS system or application login configuration." },
        { "disableColumnSanitiser", Boolean.FALSE,  "Enable optimization that disables column name sanitiser." }


     * </pre>
     */
    @Test
    public void testDatabase() {
    	
//        java -cp /usr/share/java/postgresql.jar:.
//        > -Djavax.net.ssl.keyStore=./keystore
//        > -Djavax.net.ssl.trustStore=./truststore TestJdbc
        Connection conn = null;
        try {MessageDigest md = MessageDigest.getInstance("SHA-512");
        Properties dbProperties = new Properties();
        	
//        	System.setProperty("javax.net.debug", "SSL");
//            System.setProperty("user.name", "postgres");
            String dbUrl="jdbc:postgresql://shcssolindb01.hpeswlab.net:5432/idm_gongyo3";
            String dbUser="postgres";
            String dbPassword="postgres";
            
            Class.forName(org.postgresql.Driver.class.getName());

//            String dbUrl="jdbc:postgresql://15.119.81.106:30001/postgres";
//            String dbUser="postgres";
//            String dbPassword="a/YDSm9+AQpkgQ==";
//            
////            org.postgresql.PGProperty from 9.4
            dbProperties.setProperty("user", dbUser);
            dbProperties.setProperty("password", dbPassword);
//            dbProperties.setProperty("ssl", "true");
//            dbProperties.setProperty("currentSchema", "public");
//            dbProperties.setProperty("sslfactory","org.postgresql.ssl.NonValidatingFactory");
            
            conn=   DriverManager.getConnection(dbUrl, dbProperties);
//            conn = DriverManager.getConnection("jdbc:postgresql://16.187.189.77:5432/idm_gongyo3", "postgres", "postgres");
            // printConnection(conn);

            PreparedStatement pstmt = conn.prepareStatement("select * from pg_tablespace where 1=?");
            pstmt.setInt(1, 1);
            // Statement stmt = conn.createStatement();
            // ResultSet rs = stmt.executeQuery("select * from token_store where request_token=null");
//            ResultSet rs = pstmt.executeQuery();
//            printResultSet(rs); 

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
